package com.ruoyi.rms.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.config.RuoYiConfig;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.rms.domain.DishesMessage;
import com.ruoyi.rms.service.IDishesMessageService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 菜品信息Controller
 * 
 * @author wyl
 * @date 2024-03-07
 */
@RestController
@RequestMapping("/rms/dishes")
public class DishesMessageController extends BaseController
{
    @Autowired
    private IDishesMessageService dishesMessageService;

    /**
     * 查询菜品信息列表
     */
//    @PreAuthorize("@ss.hasPermi('rms:dishes:list')")
    @GetMapping("/list")
    public TableDataInfo list(DishesMessage dishesMessage)
    {
        startPage();
        List<DishesMessage> list = dishesMessageService.selectDishesMessageList(dishesMessage);
        return getDataTable(list);
    }

    /**
     * 分组查询菜品和总销量
     */
    @GetMapping("/selectByDishesIdAndAccount")
    public AjaxResult selectByDishesIdAndAccount() {
        return AjaxResult.success(dishesMessageService.selectByDishesIdAndAccount());
    }

    /**
     * 查询菜品信息列表 不分页
     */
//    @PreAuthorize("@ss.hasPermi('rms:dishes:list')")
    @GetMapping("/allData")
    public AjaxResult allData(DishesMessage dishesMessage)
    {
        return AjaxResult.success(dishesMessageService.selectDishesMessageList(dishesMessage));
    }

    /**
     * 查询菜品信息列表 不分页 带推荐
     */
//    @PreAuthorize("@ss.hasPermi('rms:dishes:list')")
    @GetMapping("/allDataByRecommend")
    public AjaxResult allDataByRecommend(DishesMessage dishesMessage)
    {
        List<DishesMessage> dishesMessageList = dishesMessageService.selectDishesMessageList(dishesMessage);
        List<DishesMessage> recommendDishes = dishesMessageService.selectRecommendDishes();
        if (recommendDishes.isEmpty()) return AjaxResult.success(dishesMessageList);

        for (DishesMessage dishes : dishesMessageList) {
            if (recommendDishes.contains(dishes)) {
                dishes.setValue(1); // 被推荐的菜品，给一个特殊标记
            }
        }

        return AjaxResult.success(dishesMessageList);
    }

    /**
     * 查询推荐菜品
     */
    @GetMapping("/selectRecommendDishes")
    public AjaxResult selectRecommendDishes() {
        return AjaxResult.success(dishesMessageService.selectRecommendDishes());
    }

    /**
     * 导出菜品信息列表
     */
    @PreAuthorize("@ss.hasPermi('rms:dishes:export')")
    @Log(title = "菜品信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DishesMessage dishesMessage)
    {
        List<DishesMessage> list = dishesMessageService.selectDishesMessageList(dishesMessage);
        ExcelUtil<DishesMessage> util = new ExcelUtil<DishesMessage>(DishesMessage.class);
        util.exportExcel(response, list, "菜品信息数据");
    }

    /**
     * 获取菜品信息详细信息
     */
//    @PreAuthorize("@ss.hasPermi('rms:dishes:query')")
    @GetMapping(value = "/{dishesId}")
    public AjaxResult getInfo(@PathVariable("dishesId") Long dishesId)
    {
        return success(dishesMessageService.selectDishesMessageByDishesId(dishesId));
    }

    /**
     * 新增菜品信息
     */
    @PreAuthorize("@ss.hasPermi('rms:dishes:add')")
    @Log(title = "菜品信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DishesMessage dishesMessage)
    {
        return toAjax(dishesMessageService.insertDishesMessage(dishesMessage));
    }

    /**
     * 修改菜品信息
     */
    @PreAuthorize("@ss.hasPermi('rms:dishes:edit')")
    @Log(title = "菜品信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DishesMessage dishesMessage)
    {
        return toAjax(dishesMessageService.updateDishesMessage(dishesMessage));
    }

    /**
     * 删除菜品信息
     */
    @PreAuthorize("@ss.hasPermi('rms:dishes:remove')")
    @Log(title = "菜品信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{dishesIds}")
    public AjaxResult remove(@PathVariable Long[] dishesIds)
    {
        return toAjax(dishesMessageService.deleteDishesMessageByDishesIds(dishesIds));
    }
}
